//
//  ROBaseViewController.m
//  Runone
//
//  Created by runone02 on 16/12/17.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ROBaseViewController.h"
#import "NSNotificationCenter+Addition.h"
#import "ROCustomNoNetworkEmptyView.h"
#import "ROLoadingAnnimation.h"
#import "AFNetworkReachabilityManager.h"
#import "OneConst.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface ROBaseViewController ()
@property (nonatomic, weak) ROCustomNoNetworkEmptyView *noNetworkEmptyView;//无网络视图
@property (nonatomic, weak) ROLoadingAnnimation *animationView;            //加载中动画
@end

@implementation ROBaseViewController

#pragma ---------设备收费站点击

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIView setAnimationsEnabled:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout               = UIRectEdgeAll;
    self.view.backgroundColor                 = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00f];
    //请求成功的通知
    [NSNotificationCenter addObserver:self action:@selector(requestSuccessNotification) name:kRORequestSuccessNotification];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([AFNetworkReachabilityManager sharedManager].reachable == NO) {
        _noNetworkEmptyView.hidden = NO;
    } else { // 网络可用
        
        [self.noNetworkEmptyView removeFromSuperview];
        _noNetworkEmptyView = nil;
        
    }
    
}

#pragma mark - 请求成功是隐藏加载中动画
- (void)requestSuccessNotification {
        [self hideLoadingAnnimation];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)pop {
    if (self.navigationController == nil) return;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popToRootVc {
    if (self.navigationController == nil) return ;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)popToVc:(UIViewController *)vc {
    if (self.navigationController == nil) return;
    [self.navigationController popToViewController:vc animated:YES];
}

- (void)dismiss {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)dismissWithCompletion:(void (^)())completion {
    [self.navigationController dismissViewControllerAnimated:YES completion:completion];
}

-(void)presentVc:(UIViewController *)vc {
    if ([vc isKindOfClass:[UIViewController class]] == NO) return;
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void)presentVc:(UIViewController *)vc completion:(void (^)(void))completion {
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    [self presentViewController:vc animated:YES completion:completion];
}

- (void)pushVc:(UIViewController *)vc {
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)removeChildVc:(UIViewController *)childVc {
    if ([childVc isKindOfClass:[UIViewController class]] == NO) return;
    [childVc.view removeFromSuperview];
    [childVc willMoveToParentViewController:nil];
    [childVc removeFromParentViewController];
}

- (void)addChildVc:(UIViewController *)childVc {
    if ([childVc isKindOfClass:[UIViewController class]] == NO) return;
    [childVc willMoveToParentViewController:self];
    [self addChildViewController:childVc];
    [self.view addSubview:childVc.view];
    childVc.view.frame = self.view.bounds;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - lazy Load
- (ROCustomNoNetworkEmptyView *)noNetworkEmptyView {
    if(!_noNetworkEmptyView) {
        ROCustomNoNetworkEmptyView *empty = [[ROCustomNoNetworkEmptyView alloc] init];
        [self.view addSubview:empty];
        _noNetworkEmptyView = empty;
        
        __weak __typeof(self)weakSelf = self;
        [empty mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view);
        }];
        //点击回调 重新加载数据
        empty.ROCustomNoNetworkEmptyViewDidClickRetryHandle = ^(ROCustomNoNetworkEmptyView *emptyView) {
            [weakSelf loadData];
        };
    }
    return _noNetworkEmptyView;
}
#pragma mark - 展示加载动画
- (void)showLoadingAnnimation {
    ROLoadingAnnimation *animation = [[ROLoadingAnnimation alloc] init];
    [animation showInView:self.view];
    _animationView = animation;
    [self.view bringSubviewToFront:animation];
}

- (void)hideLoadingAnnimation {
    [_animationView dismiss];
    _animationView = nil;
}
- (void)loadData {
    //    [self showLoadingAnimation];
    //
    //    // 网络不可用
    //    if ([AFNetworkReachabilityManager sharedManager].reachable == NO) {
    //
    //        [self hideLoadingAnimation];
    ////        self.noNetworkEmptyView.hidden = NO;
    //
    //    } else { // 网络可用
    //
    //
    //        [self.noNetworkEmptyView removeFromSuperview];
    //        _noNetworkEmptyView = nil;
    //
    //    }
}
- (BOOL)isNetworkReachable {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
}

@end
